﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;

namespace LunchCommand.Models
{
    public class WebConfigSettings
    {
        /// <summary>
        /// The address to use as a starting point when determining travel time
        /// </summary>
        internal String StartingAddress { get; set; }

        /// <summary>
        /// The time of day the lunch starts
        /// </summary>
        internal TimeSpan LunchStartTime { get; set; }

        /// <summary>
        /// The key used for bing maps
        /// </summary>
        internal String BingMapsKey { get; set; }

        /// <summary>
        /// Exactly what it says so verbosely
        /// </summary>
        internal Int32 NumberOfDaysBeforeVenuesAreConsideredEquiallyOld { get; set; }

        /// <summary>
        /// The maximum ratio of outliers (venues with no likes nor disliked) to all venues allowed.  This limits 
        /// the likelyhood of outiers being picked when there are small groups
        /// </summary>
        internal Double MaximumLikelyhoodOfOutlierBeingPicked { get; set; }

        public static WebConfigSettings CreateFrom(NameValueCollection settings)
        {
            var ret = new WebConfigSettings();

            ret.StartingAddress = settings["StartingAddress"] ?? String.Empty;
            ret.LunchStartTime = TimeSpan.Parse(settings["LunchStartTime"] ?? "11:45:00");
            ret.BingMapsKey = settings["BingMapsKey"];

            int val;
            if (Int32.TryParse(settings["NumberOfDaysBeforeVenuesAreConsideredEquiallyOld"], out val))
            {
                ret.NumberOfDaysBeforeVenuesAreConsideredEquiallyOld = val;
            }
            else
            {
                ret.NumberOfDaysBeforeVenuesAreConsideredEquiallyOld = 14;
            }

            if (Int32.TryParse(settings["MaximumLikelyhoodOfOutlierBeingPicked"], out val))
            {
                ret.MaximumLikelyhoodOfOutlierBeingPicked = val;
            }
            else
            {
                ret.MaximumLikelyhoodOfOutlierBeingPicked = 0.1;
            }

            return ret;
        }
    }
}